/*----------------------------------------------------------------------------*/
/* Copyright 2011 http://www.kmftech.com                                      */
/*                                                                            */
/* This file is part of kmftech.                                              */
/*                                                                            */
/* kmftech is free software: you can redistribute it and/or modify it under   */
/* the terms of the GNU General Public License as published by the Free       */
/* Software Foundation, version 2 of the License, or (at your option) any     */
/* later version.                                                             */
/*                                                                            */
/* kmftech is distributed in the hope that it will be useful, but WITHOUT ANY */
/* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  */
/* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more      */
/* details.                                                                   */
/*                                                                            */
/* You should have received a copy of the GNU General Public License along    */
/* with kmftech. If not, see http://www.gnu.org/licenses/.                    */
/*----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------*/
/*---------------------------- DOCUMENT HISTORY ------------------------------*/
/* Ver: 1.0    Author: Karim Mansour                      Date: 08 June  2011 */
/* Ver: X.X    Author:                                    Date:               */
/*----------------------------------------------------------------------------*/
/*---------------------    DOCUMENT CHANGE HISTORY ---------------------------*/
/* Ver: 1.0    Change: Initial Version.                                       */
/* Ver: X.X    Change:                                                        */
/*----------------------------------------------------------------------------*/

/*                           Needed Header Files                              */
#include <avr/io.h>
#include "kmftech_types.h"

/*                         RTOS Global Declarations                           */
#define F_CPU 16000000UL
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

/*                           Functions Prototypes                             */

void vd_bsc_uart_init(void);
uint8 u8_bsc_uart_read(void);
void vd_bsc_uart_write(uint8 u8_uart_byte);

/*                         Functions Implementation                           */

/*----------------------------------------------------------------------------*/
/*---------------------------- FUNCTION HEADER -------------------------------*/
/* Name       : vd_bsc_uart_init                                              */
/* Description: Initialize the bsc uart component                             */
/* Input(s)   : Input  1: void                                                */
/* Output(s)  : Output 1: void                                                */
/* Author     : Karim Mansour                                                 */
/*----------------------------------------------------------------------------*/
void vd_bsc_uart_init(void)
{
   UBRR0L = (uint8_t)(BAUD_PRESCALLER);
   UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);
   UCSR0B = (1<<RXEN0)|(1<<TXEN0);
   UCSR0C = (3<<UCSZ00);
}


/*----------------------------------------------------------------------------*/
/*---------------------------- FUNCTION HEADER -------------------------------*/
/* Name       : u8_bsc_uart_read                                              */
/* Description: Read byte from the UART                                       */
/* Input(s)   : Input  1: void                                                */
/* Output(s)  : Output 1: uint8 - read byte                                   */
/* Author     : Karim Mansour                                                 */
/*----------------------------------------------------------------------------*/
uint8 u8_bsc_uart_read(void)
{
   while(!(UCSR0A & (1<<RXC0)));
   return UDR0;
}

/*----------------------------------------------------------------------------*/
/*---------------------------- FUNCTION HEADER -------------------------------*/
/* Name       : u8_bsc_uart_write                                             */
/* Description: Write byte to the UART                                        */
/* Input(s)   : Input  1: uint8 - Byte to be written on the uart bus          */
/* Output(s)  : Output 1: void                                                */
/* Author     : Karim Mansour                                                 */
/*----------------------------------------------------------------------------*/
void vd_bsc_uart_write(uint8 u8_uart_byte)
{
   while(!(UCSR0A & (1<<UDRE0)));
   UDR0 = u8_uart_byte;
}

/*-----------------------------End Of File------------------------------------*/